package com.bc.bczbweb.controller.Ajax;

import com.bc.bcapi.service.T_Authority_infoService;
import com.bc.bcapi.service.T_Mnguser_infoService;
import com.bc.bcapi.service.T_Role_infoService;
import com.bc.bcmodel.consts.GlobalSettings;
import com.bc.bcmodel.domain.T_Authority_infoModel;
import com.bc.bcmodel.domain.T_Mnguser_infoModel;
import com.bc.commoncore.security.AES;
import com.bc.commoncore.util.CookieUtils;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Name AjaxManageUserController
 * @Description
 * @Author douhaichao
 * @Date 2018/8/14 12:27
 * @Version 1.0
 **/
@RestController
@RequestMapping("ajax/manage")
public class AjaxManageUserController {

    @Autowired
    T_Mnguser_infoService t_mnguser_infoService;
    @Autowired
    T_Authority_infoService t_authority_infoService;
    @Autowired
    T_Role_infoService t_role_infoService;

    @RequestMapping(value = "login", method = {RequestMethod.POST})
    public JsonResult login(T_Mnguser_infoModel req, HttpServletRequest request, HttpServletResponse response) {
        String pwd = AES.aesEncrypt(req.getPassword());
        req.setPassword(pwd);
        req.setStatus("1");
        T_Mnguser_infoModel resultModel = t_mnguser_infoService.findByNamedParam(req);
        JsonResult js = new JsonResult();
        if (resultModel == null) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }

        /*获取所有菜单列表开始*/
        T_Authority_infoModel reqAuth = new T_Authority_infoModel();
        reqAuth.setRole_id(resultModel.getRole_id());
        List<T_Authority_infoModel> listRoles = t_authority_infoService.getAllList(reqAuth);

        String role_ids = "";
        for (T_Authority_infoModel item :
                listRoles) {
            role_ids += item.getMenu_id() + ",";
        }
        resultModel.setPassword("");
        resultModel.setCreate_time(null);
        resultModel.setModify_time(null);
        resultModel.setRole_name(t_role_infoService.getRoleNameById(resultModel.getRole_id()));
        resultModel.setRole_id(role_ids);
        /*获取所有菜单列表结束*/
        String resultStr = JsonUtil.obj2string(resultModel);
        resultStr = AES.aesEncrypt(resultStr);
        CookieUtils.setCookie(request, response, GlobalSettings.MANAGE_COOKIENAME, resultStr);
        return js;
    }
}
